package com.huilian.tool.work.weixin.service.impl;

import com.huilian.tool.common.utils.RestTemplateUtil;
import com.huilian.tool.work.weixin.dto.CoprTag;
import com.huilian.tool.work.weixin.dto.ExternalContact;
import com.huilian.tool.work.weixin.service.TabService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 少凡
 * @Date 2022-06-27 17:48
 * version 1.0
 */
@Service
public class TabServiceImpl implements TabService {

    @Resource
    private RestTemplateUtil restTemplateUtil;


    @Override
    public String getCorpTagList(String accessToken) {

        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?")
                .append("access_token=").append(accessToken).toString();
        return restTemplateUtil.unifyPost(url, null, String.class);
    }

    @Override
    public String addCorpTag(String accessToken, CoprTag coprTag) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?")
                .append("access_token=").append(accessToken).toString();
        return restTemplateUtil.unifyPost(url, coprTag, String.class);
    }

    @Override
    public String editCorpTag(String accessToken, String id, String name) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag?")
                .append("access_token=").append(accessToken).toString();
        Map<String,String> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        return restTemplateUtil.unifyPost(url, map, String.class);
    }

    @Override
    public String delCorpTag(String accessToken, List<String> tagIdList, List<String> groupIds) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?")
                .append("access_token=").append(accessToken).toString();
        Map<String,List<String>> map = new HashMap<>();
        if(tagIdList != null && tagIdList.size() >0){
            map.put("tag_id",tagIdList);
        }
        if(groupIds != null && groupIds.size() >0){
            map.put("group_id",groupIds);
        }
        return restTemplateUtil.unifyPost(url, map, String.class);
    }

    @Override
    public String markTag(String accessToken, ExternalContact contact) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?")
                .append("access_token=").append(accessToken).toString();
        return restTemplateUtil.unifyPost(url, contact, String.class);
    }
}
